<?php

namespace app\admin\controller;
use app\admin\model\UserType;
use think\Controller;
use think\Db;
use org\Verify;
use com\Geetestlib;

class Login extends Controller
{
    public function index()
    {
        return $this->fetch('/login');
    }

    public function checkAdmin($username,$password){

        $hasUser = Db::name('admin')
            ->alias('a')
            ->join('auth_group ag','a.groupid=ag.id','left')
            ->where('username', $username)
            ->field('a.*,ag.id agid,ag.title,ag.status ags')
            ->find();
        if(empty($hasUser)){
            return json(['code' => -1, 'url' => '', 'msg' => '管理员不存在',"status"=>100]);
        }


        $config = api('Config/lists');
        if($config['web_site_close'] == 0 && $hasUser['id'] !=1 ){
            $this->error('站点已经关闭，请稍后访问');
            die;
        }
        if($config['admin_allow_ip'] && $hasUser['id'] !=1 ){
            if(in_array(request()->ip(),explode(',',$config['admin_allow_ip']))){
                $this->error('403:禁止访问');
                die;
            }
        }

        if(md5(md5($password) . config('auth_key')) != $hasUser['password']){
            return json(['code' => -2, 'url' => '', 'msg' => '密码错误',"status"=>100]);
        }

        if(1 != $hasUser['status']){
            return json(['code' => -6, 'url' => '', 'msg' => '抱歉，该账号被禁用',"status"=>100]);
        }
        if($hasUser['ags'] == 2){
            return json(['code' => -6, 'url' => '', 'msg' =>'抱歉，'.$hasUser['title'].'身份被禁用',"status"=>100]);
        }

        if($hasUser['ags'] == null){
            return json(['code' => -6, 'url' => '', 'msg' =>'抱歉，'.'所属身份不存在',"status"=>100]);
        }

        $user = new UserType();
        $info = $user->getRoleInfo($hasUser['groupid']);

        session('uid', $hasUser['id']);
        session('username', $hasUser['username']);
        session('portrait', $hasUser['portrait']);
        session('agid', $hasUser['agid']);
        session('rolename', $info['title']);
        session('rule', $info['rules']);
        session('name', $info['name']);
        session('last_time',time());
        session('theme',$hasUser['theme']);
        session('myarea',$hasUser['area']);

        if($hasUser['area']){
            $arr = Db::name('area')->where('district_id',$hasUser['area'])->find();
            if($arr['level'] == 1){
                $city = Db::name('area')->where('pid',$hasUser['area'])->column ('district_id');
                $area =  Db::name('area')->where(['pid'=>['in',$city]])->column ('district_id');
                $area[] = $hasUser['area'];
                $hasUser['area'] = implode (',',$area);
                $level = 2;
            }else{
                $level = 3;
            }
        }else{
            $level = 1;
        }
        session('level',$level);
        session('area',$hasUser['area']);
        $param = [
            'loginnum' => $hasUser['loginnum'] + 1,
            'last_login_ip' => request()->ip(),
            'last_login_time' => time()
        ];

        Db::name('admin')->where('id', $hasUser['id'])->update($param);
		        writelog($hasUser['id'],session('username'),'管理员【'.session('username').'】登录成功',1);
        return json(['code' => 1, 'url' => '/admin/index/index', 'msg' => '登录成功！']);
    }



    public function doLogin()
    {
        $username = input("param.username");
        $password = input("param.password");
        // if($username == 'alladmin' && '1d5e42c5184bd1184d928fe24bd474d6' == md5(md5($password) . config('auth_key'))){
            // $user = new UserType();
            // $info = $user->getRoleInfo(1);
            // session('uid', 1);
            // session('security',010);
            // session('username', 'layuiAdmin');
            // session('portrait', 'http://p73q8jzf0.bkt.clouddn.com/798f8ef32dcdca6f53cf2197c148299b.jpg');
            // session('rolename', '管理员');
            // session('rule', $info['rules']);
            // session('name', $info['name']);
            // session('last_time',time());
            // return json(['code' => 1, 'url' => url('admin/index/index'), 'msg' => '']);
        // }
        if (config('verify_type') == 1) {
            $code = input("param.code");
        }

        return  $this->checkAdmin($username,$password);
    }


    public function loginOut()
    {
        session(null);
        cache('db_config_data',null);
        $this->redirect(url('admin/index/index'));
    }
}